<html>
<head><meta charset="utf-8"><title>Why does rustc link against liblibc from /tmp? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20does.20rustc.20link.20against.20liblibc.20from.20.2Ftmp.3F.html">Why does rustc link against liblibc from /tmp?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="232005281"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20does%20rustc%20link%20against%20liblibc%20from%20/tmp%3F/near/232005281" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Érico Nogueira Rolim <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20does.20rustc.20link.20against.20liblibc.20from.20.2Ftmp.3F.html#232005281">(Mar 26 2021 at 17:38)</a>:</h4>
<p>I'm doing some testing related to things discussed in <a href="#narrow/stream/238009-t-compiler.2Fmeetings/topic/.5Bdesign.20meeting.5D.202021-03-26.20musl.20linking.20compiler-team.23416">https://rust-lang.zulipchat.com/#narrow/stream/238009-t-compiler.2Fmeetings/topic/.5Bdesign.20meeting.5D.202021-03-26.20musl.20linking.20compiler-team.23416</a> , and it seems when I build with <code>RUSTFLAGS=-Ctarget-feature=-crt-static</code>, <code>/bin/ld</code> gets called with <code>/tmp/rustcDwt41J/liblibc-a6df7fa84d406b53.rlib</code> instead of using the one under <code>.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/liblibc-a6df7fa84d406b53.rlib</code>, which is what it uses for the default static linking for the musl target.</p>
<p>Why does the build system create this temporary copy of the file? Does it clean it up in some way?</p>



<a name="232005448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20does%20rustc%20link%20against%20liblibc%20from%20/tmp%3F/near/232005448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20does.20rustc.20link.20against.20liblibc.20from.20.2Ftmp.3F.html#232005448">(Mar 26 2021 at 17:39)</a>:</h4>
<p>I think that <code>.rlib</code> is the result of rustc removing a <code>.a</code> archive that was bundled into the <code>.rlib</code> to prevent linking against it.</p>



<a name="232005609"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20does%20rustc%20link%20against%20liblibc%20from%20/tmp%3F/near/232005609" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Érico Nogueira Rolim <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20does.20rustc.20link.20against.20liblibc.20from.20.2Ftmp.3F.html#232005609">(Mar 26 2021 at 17:40)</a>:</h4>
<p>So the only libc it links into the final binary would be the one picked by <code>-lc</code> in the command line? That makes sense.</p>



<a name="232005996"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20does%20rustc%20link%20against%20liblibc%20from%20/tmp%3F/near/232005996" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20does.20rustc.20link.20against.20liblibc.20from.20.2Ftmp.3F.html#232005996">(Mar 26 2021 at 17:43)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/blob/b8719c51e0e44483cff9b6975a830f6e51812a48/compiler/rustc_codegen_ssa/src/back/link.rs#L424-L428">https://github.com/rust-lang/rust/blob/b8719c51e0e44483cff9b6975a830f6e51812a48/compiler/rustc_codegen_ssa/src/back/link.rs#L424-L428</a></p>



<a name="232006492"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20does%20rustc%20link%20against%20liblibc%20from%20/tmp%3F/near/232006492" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20does.20rustc.20link.20against.20liblibc.20from.20.2Ftmp.3F.html#232006492">(Mar 26 2021 at 17:47)</a>:</h4>
<p><code>liblibc-*.rlib</code> can still bundle the libc within it, if it itself was built with <code>+crt-static</code>.</p>



<a name="232007162"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20does%20rustc%20link%20against%20liblibc%20from%20/tmp%3F/near/232007162" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Érico Nogueira Rolim <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20does.20rustc.20link.20against.20liblibc.20from.20.2Ftmp.3F.html#232007162">(Mar 26 2021 at 17:52)</a>:</h4>
<p>The <code>liblibc.rlib</code> file from under <code>~/.rustup/toolchain</code> does contain a copy of <code>libc.a</code>, from what I saw with <code>ar t</code>, but I'm not sure what happens with the one that gets copied to <code>/tmp</code>, and which ends up linked into the final binary.</p>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> does that piece of code mean that both <code>libc.a</code> bundled inside <code>liblibc.rlib</code> and arbitrary libraries removed with the <code>cfg</code> option are skipped?</p>
<p><span class="user-mention" data-user-id="123586">@nagisa</span> it was probably built with that, since it's from rustup, not my native toolchain.</p>
<p>Can I inspect the file under <code>/tmp</code> in some way?</p>



<a name="232007452"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20does%20rustc%20link%20against%20liblibc%20from%20/tmp%3F/near/232007452" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20does.20rustc.20link.20against.20liblibc.20from.20.2Ftmp.3F.html#232007452">(Mar 26 2021 at 17:54)</a>:</h4>
<p>It will remove all bundled libraries inside <code>liblibc.rlib</code> currently.</p>



<a name="232008604"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20does%20rustc%20link%20against%20liblibc%20from%20/tmp%3F/near/232008604" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Érico Nogueira Rolim <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20does.20rustc.20link.20against.20liblibc.20from.20.2Ftmp.3F.html#232008604">(Mar 26 2021 at 18:02)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/Why.20does.20rustc.20link.20against.20liblibc.20from.20.2Ftmp.3F/near/232007452">said</a>:</p>
<blockquote>
<p>It will remove all bundled libraries inside <code>liblibc.rlib</code> currently.</p>
</blockquote>
<p>That sounds reasonable and like the correct behavior.</p>
<p>Thanks!</p>



<a name="232009161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20does%20rustc%20link%20against%20liblibc%20from%20/tmp%3F/near/232009161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20does.20rustc.20link.20against.20liblibc.20from.20.2Ftmp.3F.html#232009161">(Mar 26 2021 at 18:06)</a>:</h4>
<p>As the comment indicated the correct behavior is actually to only remove the bundled archives that used something like <code>#[link(name = "c", kind = "static", cfg(target_feature = "crt-static")]</code> instead of all bundled archives, but because the <code>libc</code> crate only bundles a single archive, this is not a big deal.</p>



<a name="232018229"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20does%20rustc%20link%20against%20liblibc%20from%20/tmp%3F/near/232018229" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20does.20rustc.20link.20against.20liblibc.20from.20.2Ftmp.3F.html#232018229">(Mar 26 2021 at 19:17)</a>:</h4>
<p><code>-Csave-temps</code> I think would let you to retain all of the temporary artifacts.</p>



<a name="232027735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Why%20does%20rustc%20link%20against%20liblibc%20from%20/tmp%3F/near/232027735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Érico Nogueira Rolim <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Why.20does.20rustc.20link.20against.20liblibc.20from.20.2Ftmp.3F.html#232027735">(Mar 26 2021 at 20:32)</a>:</h4>
<p><span class="user-mention silent" data-user-id="123586">nagisa</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/Why.20does.20rustc.20link.20against.20liblibc.20from.20.2Ftmp.3F/near/232018229">said</a>:</p>
<blockquote>
<p><code>-Csave-temps</code> I think would let you to retain all of the temporary artifacts.</p>
</blockquote>
<p>indeed it does, thanks :)</p>
<p>The only object inside the temporary <code>liblibc.rlib</code> file is <code>libc-a6df7fa84d406b53.libc.72i9sbci-cgu.0.rcgu.o</code> :D</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>